﻿/*
 * HCC Robotica, Workshop Robot programmeren met C# 
 * Oefening : wo_04	Geluiden genereren voor piezo
 * Naam     : Hinnie
 * Versie   : september 2011
 */

using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;

namespace wo_04
{
    public class wo_04
    {
        public static void Main()
        {
            OutputPort LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
            InputPort ButtonLDR = new InputPort((Cpu.Pin)FEZ_Pin.Digital.LDR, false, Port.ResistorMode.PullUp);
            InputPort ButtonDi2 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di2, false, Port.ResistorMode.Disabled);
            InputPort ButtonDi4 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di4, false, Port.ResistorMode.Disabled);
            bool ledState = false;
            bool ledSwitch = false;
            const int loopTime = 5;
            const int flashTime = 200 / loopTime;

            bool butLDR = true;
            bool butDi2 = true;
            bool butDi4 = true;
            int flashCount = 0;
            while (true)
            {
                if (ButtonLDR.Read() != butLDR)
                {
                    butLDR = ButtonLDR.Read();
                    if (!butLDR)
                        FEZMini_Robot.Beep(8000, 100);
                }
                else if (ButtonDi2.Read() != butDi2)
                {
                    butDi2 = ButtonDi2.Read();
                    if (!butDi2)
                        FEZMini_Robot.Beep(7600, 100);
                }
                else if (ButtonDi4.Read() != butDi4)
                {
                    butDi4 = ButtonDi4.Read();
                    if (!butDi4)
                        FEZMini_Robot.Beep(7200, 100);
                }
                if (flashCount++ >= flashTime)
                {
                    flashCount = 0;
                    ledState = !ledState;    		// toggle LED state
                    LED.Write(ledState);     		// blink onboard LED
                }
                if (ledState)
                {
                    ledSwitch = !ledSwitch;
                    LED.Write(ledSwitch);           // less intensity onboard LED
                }
                Thread.Sleep(loopTime);
            }
        }
    }
}
